package com.rockcent.helper;

import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.Category;
import com.rockcent.mall.lib.repository.CategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by DK on 16/10/25.
 */
@Component
public class CategoryHelper {
    @Autowired
    private CategoryRepository categoryRepository;

    public List<Long> categoryIds(Long mallId, List<Long> categoryIdList, List<Long> resultCategoryIdList) {
        if (resultCategoryIdList == null) {
            resultCategoryIdList = new ArrayList<>();
        }
        if (categoryIdList == null || categoryIdList.size() == 0) {
            return resultCategoryIdList;
        }
        List<Category> categoryList = categoryRepository.findByMallIdAndVisibleAndIsDeleteAndParentIdIn(mallId, true, Whether.NO, categoryIdList);

        if (categoryList.size() == 0) {
            return new ArrayList<>();
        }
        for (int i = 0; i < categoryList.size(); i++) {
            Category category = categoryList.get(i);
            if (category.getParentId() != null) {
                List<Long> categoryList1 = this.categoryIds(mallId, Arrays.asList(category.getId()), resultCategoryIdList);
                if (categoryList1.size() == 0) {
                    resultCategoryIdList.add(category.getId());
                }
            }
        }
        return resultCategoryIdList;
    }
}